package org.hxyjs.service.impl;


import org.hxyjs.dao.SubjectDao;
import org.hxyjs.dto.DTO;
import org.hxyjs.dto.MessageEnum;
import org.hxyjs.entity.Subject;
import org.hxyjs.page.PageInfo;
import org.hxyjs.service.SubjectService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

@Service("SubjectService")
public class SubjectServiceImpl implements SubjectService {
    @Resource
    SubjectDao subjectDao;
    @Override
    public DTO<Subject> getAllSubjects()  throws Exception{
        List<Subject> subjects = subjectDao.getAllSubjects();
        MessageEnum messageEnum  = Optional.ofNullable(subjects).isPresent()?MessageEnum.GETSUBJECTSUCCESS: MessageEnum.GETSUBJECTFAIL;
        return new DTO<Subject>(messageEnum.getCode(),messageEnum.getMessage(),subjects);
    }

    @Override
    public DTO<Subject> deleteSubjectBySjno(String sno)  throws Exception{
        Integer result = subjectDao.deleteSubjectBySjno(sno);
        MessageEnum messageEnum  =result> 0?MessageEnum.DELETESUBJECTSUCCESS:MessageEnum.DELETESUBJECTFAIL;
        return new DTO<Subject>(messageEnum.getCode(),messageEnum.getMessage());
    }

    @Override
    public DTO<Subject> updateSubjectBySjno(Subject subject)  throws Exception{
        Integer result = subjectDao.updateSubjectBySjno(subject);
        MessageEnum messageEnum  =result> 0?MessageEnum.UPDATESUBJECTSUCCESS:MessageEnum.UPDATESUBJECTFAIL;
        return new DTO<Subject>(messageEnum.getCode(),messageEnum.getMessage());
    }

    @Override
    public DTO<Subject> addSubject(Subject subject) throws Exception {
        Integer result = subjectDao.addSubject(subject);
        MessageEnum messageEnum  = result> 0?MessageEnum.ADDSUBJECTSUCCESS:MessageEnum.ADDSUBJECTFAIL;
        return new DTO<Subject>(messageEnum.getCode(),messageEnum.getMessage());
    }
    @Override
    public DTO<Subject> getSubjectByPage(PageInfo pageInfo ) throws Exception {
        List<Subject> subjects = subjectDao.getSubjectByPage(pageInfo);
        MessageEnum messageEnum  = Optional.ofNullable(subjects).isPresent()?MessageEnum.GETSUBJECTSUCCESS: MessageEnum.GETSUBJECTFAIL;
        return new DTO<Subject>(messageEnum.getCode(),messageEnum.getMessage(),subjects,pageInfo);
    }
}
